﻿@page "/varcard/{id:int}"
@using TrackSystem.Entities.Dtos.Var
@using TrackSystem.Entities.Extensions
@using TrackSystem.UI.Server.Units

<h1>变量列表</h1>

<Divider class="mt-3 mb-3" />

<div class="row g-3">

    @{ var curSerial = VarSerialEnum.Sensor;  }

    @* 已有变量显示 *@
    @foreach (var Reg in Regs ?? Enumerable.Empty<VarDto>())
    {
        <div class="col-12 col-sm-6 col-lg-4 col-xl-2">
            <Card Color="Color.Info" IsCenter="true">
                <HeaderTemplate>
                    传感器名称：@Reg.Name
                </HeaderTemplate>

                <BodyTemplate>
                    @if (changedStatus.TryGetValue(Reg.Id, out var isChanged) && isChanged)
                    {

                    }
                    else
                    {
                        <VarForView Reg="Reg" />
                    }
                </BodyTemplate>

                <FooterTemplate>
                    @if (changedStatus.TryGetValue(Reg.Id, out var isChanged) && isChanged)
                    {
                        <Button class="btn btn-primary" OnClick="@(() => OnChanged(Reg.Id))">
                            <i class="fa-solid fa-check"></i>
                        </Button>
                        <Button class="btn btn-primary" OnClick="@(() => OnChangedCancel(Reg.Id))">
                            <i class="fa-solid fa-xmark"></i>
                        </Button>
                    }
                    else
                    {
                        <Button class="btn btn-primary" OnClick="@(() => OnStartChange(Reg.Id))">
                            <i class="fa-solid fa-hammer"></i>
                        </Button>
                    }

                    <PopConfirmButton Placement="Placement.Top"
                                      Color="Color.Danger"
                                      ConfirmIcon="fa-solid fa-triangle-exclamation text-danger"
                                      ConfirmButtonColor="Color.Danger"
                                      Text=""
                                      Content="确定删除数据吗？"
                                      OnConfirm="@(() => OnDeleted(Reg.Id))">
                        <i class="fa-solid fa-delete-left"></i>
                    </PopConfirmButton>

                </FooterTemplate>
            </Card>
        </div>

        @if (curSerial != Reg.Serial)
        {
            <Divider class="mt-3 mb-3" />
            curSerial = Reg.Serial;
        }
    }

    @* 新增变量 *@
    <div class="col-12 col-sm-6 col-lg-4 col-xl-2">
        <Card Color="Color.Primary" IsCenter="true">
            <HeaderTemplate>
                新增传感器
            </HeaderTemplate>

            <BodyTemplate>
                @if (IsInsertMode && regForInsert != null)
                {
                    @* <SensorForUpdate Sensor="regForInsert" /> *@
                }
                else
                {
                    <img src="images/add.jpg" width="100" height="100" onclick="@OnStartInsert"/>
                }
            </BodyTemplate>

            <FooterTemplate>
                @if (IsInsertMode && regForInsert != null)
                {
                <Button class="btn btn-primary" OnClick="@OnFinishInsert">
                    <i class="fa-solid fa-check"></i>
                </Button>
                <Button class="btn btn-secondary" OnClick="@(() => IsInsertMode = false)">
                    <i class="fa-solid fa-xmark"></i>
                </Button>
                }
            </FooterTemplate>
        </Card>

    </div>

</div>

<Divider class="mt-3 mb-3" />

@* 分页组件 *@
<Pagination PageCount="@Regs.Count()" OnPageLinkClick="@OnPageClick" Alignment="Alignment.Center" class="mt-3" />

